package io.ouka.pub.response;


import lombok.Builder;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author ouka
 * @ClassName ResultMsg
 * @Package com.sinosig.publicmodule.unifiedreturn
 * @Description: 返回值
 * @date 2020/5/19 16:09
 */
@Data
@Builder
@ToString
public class ResultMsg<T, DOMAIN> implements Serializable {
    public enum ExceptionType{
        SYS_EXCEPTION,
        BUS_EXCEPTION
    }
    private static final Integer DEFAULT_BUSSUCESS_HTTPCODE = 200;
    private static final Integer DEFAULT_BUSFAIL_HTTPCODE = 400;
    private static final Integer DEFAULT_SYSFAIL_HTTPCODE = 500;
    private static final Integer DEFAULT_SUCESS_CODE = 200;
    private static final Integer DEFAULT_FAIL_CODE = 500;
    private static final String DEFAULT_SUCESS_MSG = "成功";
    private static final String DEFAULT_FAIL_MSG = "失败";
    private Integer code;
    private T data;
    private DOMAIN msg;
    /**
     * 成功返回
     *
     * @param data 数据
     * @param msg  提示 缺省值为 成功
     * @param <T>  数据类型
     * @return 返回结果
     */
    public static <T, DOMAIN> ResultMsg wrapperBusSuccessResult(T data, DOMAIN msg) {
        if (null == msg) {
            return ResultMsg.builder().code(DEFAULT_SUCESS_CODE).msg(DEFAULT_SUCESS_MSG).data(data).build();
        }
        return ResultMsg.builder().code(DEFAULT_SUCESS_CODE).msg(msg).data(data).build();
    }

    /**
     * 失败返回
     *
     * @param code 状态码 缺省值为 200
     * @param msg  提示信息 缺省值为 失败
     * @return 返回结果
     */
    public static <DOMAIN> ResultMsg wrapperFailResult(Integer code, DOMAIN msg, ExceptionType type) {
        if (null == code) {
            return ResultMsg.builder().code(DEFAULT_FAIL_CODE).msg(msg).build();
        }
        if (null == msg) {
            return ResultMsg.builder().code(code).msg(DEFAULT_FAIL_MSG).build();
        }
        return ResultMsg.builder().code(code).msg(msg).build();
    }

}
